package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatCircleSportsman;
import com.happy.cat.project.system.service.ICatCircleSportsmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 圈子选手Controller
 *
 * @author verne
 * @date 2020-07-22
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/circleSportsman" )
@Api("圈子选手 API")
public class CatCircleSportsmanController extends BaseController {

    private final ICatCircleSportsmanService iCatCircleSportsmanService;

    /**
     * 查询圈子选手列表
     */
    @ApiOperation(value = "查询圈子选手列表",notes = "查询圈子选手列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:circleSportsman:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatCircleSportsman catCircleSportsman)
    {
        startPage();
        LambdaQueryWrapper<CatCircleSportsman> lqw = new LambdaQueryWrapper<CatCircleSportsman>();
        if (catCircleSportsman.getUserId() != null){
            lqw.eq(CatCircleSportsman::getUserId ,catCircleSportsman.getUserId());
        }
        if (catCircleSportsman.getCircleId() != null){
            lqw.eq(CatCircleSportsman::getCircleId ,catCircleSportsman.getCircleId());
        }
        if (catCircleSportsman.getStatus() != null){
            lqw.eq(CatCircleSportsman::getStatus ,catCircleSportsman.getStatus());
        }
        List<CatCircleSportsman> list = iCatCircleSportsmanService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出圈子选手列表
     */
    @ApiOperation(value = "导出圈子选手列表",notes = "导出圈子选手列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circleSportsman:export')" )
    @Log(title = "圈子选手" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatCircleSportsman catCircleSportsman) {
        LambdaQueryWrapper<CatCircleSportsman> lqw = new LambdaQueryWrapper<CatCircleSportsman>(catCircleSportsman);
        List<CatCircleSportsman> list = iCatCircleSportsmanService.list(lqw);
        ExcelUtil<CatCircleSportsman> util = new ExcelUtil<CatCircleSportsman>(CatCircleSportsman. class);
        return util.exportExcel(list, "circleSportsman" );
    }

    /**
     * 获取圈子选手详细信息
     */
    @ApiOperation(value = "获取圈子选手详细信息",notes = "获取圈子选手详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circleSportsman:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatCircleSportsmanService.getById(id));
    }

    /**
     * 新增圈子选手
     */
    @ApiOperation(value = "新增圈子选手",notes = "新增圈子选手",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circleSportsman:add')" )
    @Log(title = "圈子选手" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatCircleSportsman catCircleSportsman) {
        catCircleSportsman.setCreateBy(SecurityUtils.getUsername());
        catCircleSportsman.setCreateTime(new Date());
        return toAjax(iCatCircleSportsmanService.save(catCircleSportsman) ? 1 : 0);
    }

    /**
     * 修改圈子选手
     */
    @ApiOperation(value = "修改圈子选手",notes = "修改圈子选手",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circleSportsman:edit')" )
    @Log(title = "圈子选手" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatCircleSportsman catCircleSportsman) {
        return toAjax(iCatCircleSportsmanService.updateById(catCircleSportsman) ? 1 : 0);
    }

    /**
     * 删除圈子选手
     */
    @ApiOperation(value = "删除圈子选手",notes = "删除圈子选手",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circleSportsman:remove')" )
    @Log(title = "圈子选手" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatCircleSportsmanService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
